Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
koa-compress
Advanced tools
The koa-compress package is a middleware for the Koa framework that provides HTTP compression. It supports various compression algorithms like gzip, deflate, and brotli, which can significantly reduce the size of the response body and improve the performance of web applications.
Basic Gzip Compression
This code demonstrates how to set up basic gzip compression in a Koa application using the koa-compress middleware. The middleware is added to the Koa app, and it will automatically compress the response body using gzip.
const Koa = require('koa');
const compress = require('koa-compress');
const app = new Koa();
app.use(compress());
app.use(ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
Custom Compression Options
This code shows how to configure custom options for the compression middleware. The 'threshold' option specifies the minimum response size in bytes to compress, and the 'flush' option is used to control the zlib flush mode.
const Koa = require('koa');
const compress = require('koa-compress');
const app = new Koa();
app.use(compress({
threshold: 2048,
flush: require('zlib').Z_SYNC_FLUSH
}));
app.use(ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
Brotli Compression
This code demonstrates how to enable Brotli compression with custom parameters. The 'br' option is used to configure Brotli-specific settings, such as the compression quality.
const Koa = require('koa');
const compress = require('koa-compress');
const zlib = require('zlib');
const app = new Koa();
app.use(compress({
br: {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 4
}
}
}));
app.use(ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
The 'compression' package is a middleware for Express.js that provides HTTP compression. It supports gzip and deflate algorithms. Compared to koa-compress, it is designed specifically for Express.js and does not support Brotli compression out of the box.
The 'shrink-ray-current' package is a middleware for Node.js that provides HTTP compression using gzip, deflate, and Brotli algorithms. It is similar to koa-compress in terms of supported algorithms but can be used with various frameworks, not just Koa.
Compress middleware for Koa
const compress = require('koa-compress')
const Koa = require('koa')
const app = new Koa()
app.use(compress({
filter (content_type) {
return /text/i.test(content_type)
},
threshold: 2048,
gzip: {
flush: require('zlib').constants.Z_SYNC_FLUSH
},
deflate: {
flush: require('zlib').constants.Z_SYNC_FLUSH,
},
br: false // disable brotli
}))
function (mimeType: string): Boolean {
}
An optional function that checks the response content type to decide whether to compress. By default, it uses compressible.
Minimum response size in bytes to compress.
Default 1024
bytes or 1kb
.
The current encodings are, in order of preference: br
, gzip
, deflate
.
Setting options[encoding] = {}
will pass those options to the encoding function.
Setting options[encoding] = false
will disable that encoding.
Brotli compression is supported in node v11.7.0+, which includes it natively. As of v5.1.0, the default quality level is 4 for performance reasons.
An optional string, which specifies what encoders to use for requests without
Accept-Encoding.
Default identity
.
The standard dictates to treat such requests as *
meaning that all compressions are permissible,
yet it causes very practical problems when debugging servers with manual tools like curl
, wget
, and so on.
If you want to enable the standard behavior, just set defaultEncoding
to *
.
You can always enable compression by setting ctx.compress = true
.
You can always disable compression by setting ctx.compress = false
.
This bypasses the filter check.
app.use((ctx, next) => {
ctx.compress = true
ctx.body = fs.createReadStream(file)
})
FAQs
Compress middleware for koa
The npm package koa-compress receives a total of 329,126 weekly downloads. As such, koa-compress popularity was classified as popular.
We found that koa-compress demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 11 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.